/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package tcpserver;

import java.io.BufferedInputStream;
import java.io.DataInputStream;
import java.io.IOException;

/**
 *
 * @author makkalot
 */

import java.util.logging.Level;
import java.util.logging.Logger;
import udpserver.LoadImage;

public class TestClient extends GeneralTcpClient{

    private int send_status = 0;  //0 is start 1 is continue -1 is stop
    private LoadImage load_video;
    
    public TestClient() throws IOException{
        super();
    }
    
    @Override
    public void sendData(String file_to_send)  {
        try{
       
       //ole iste 
        while(this.send_status!=-2){
        //System.out.println("The status is :"+this.send_status);
        if (this.send_status==0){
            
            //this.out.writeBytes(theLine.getBytes(), 0, theLine.getBytes().length);
            this.out.write("start".getBytes(), 0,"start".getBytes().length );
            //this.out.writeBytes(theLine);
            this.out.flush(); //send all
            System.out.println("I sent the start mesage");
            
            byte[] buffer_send = new byte[GeneralTcpServer.MAX_IO_BUFF];
            
            while(this.in.read(buffer_send)!=-1){
                String tem_str = new String(buffer_send,0,buffer_send.length);
                tem_str = tem_str.trim();
                
                if(tem_str.equals("start")){
                    System.out.println("Server says :"+tem_str);
                    this.send_status=1;
                    this.load_video = new LoadImage(file_to_send);
                    break;
                }
                else
                    break;
        
            }
            
            
        }
        else if(this.send_status==1){
            System.out.println("Send here the continue mesage");
            //this.out.write("continue\n\r".getBytes());
            byte[] buffer_send = new byte[GeneralTcpServer.MAX_IO_BUFF];
            
            while(this.load_video.readThatByte(buffer_send, GeneralTcpServer.MAX_IO_BUFF)!=-1){
                
                this.out.write(buffer_send, 0,buffer_send.length);
                this.out.flush(); //send all
                
                       /* try {
                            //send all
                            Thread.sleep(5);
                        } catch (InterruptedException ex) {
                            Logger.getLogger(TestClient.class.getName()).log(Level.SEVERE, null, ex);
                        }
                        * */
                buffer_send = null; // we dont want old chunks
                buffer_send = new byte[GeneralTcpServer.MAX_IO_BUFF];
            
            }
            //burada hep gonder
            this.send_status = -1;
        }
        
        else if(send_status==-1){
            System.out.println("Sending the final mesage");
            //this.out.write("stop\n\r".getBytes());
            this.out.write("stop".getBytes(), 0,"stop".getBytes().length );
            this.out.flush(); //send all
            
            byte[] buffer_send = new byte[GeneralTcpServer.MAX_IO_BUFF];
            while(this.in.read(buffer_send)!=-1){
                String tem_str = new String(buffer_send,0,buffer_send.length);
                tem_str = tem_str.trim();
                
                if(tem_str.equals("stop")){
                    System.out.println("Server says :"+tem_str);
                    this.send_status = -2;
                    this.load_video.finishReading();
                    try{
                        this.out.close(); // if that is what they mean
                        this.kksocket.close();
                    }
                    catch(IOException e){
                System.out.println("Korkma hata verme gerek yok");
                
                    }
                    break;
                }
                else
                    break;
        
            }
            
           }
        
       
        }//while sonu
        this.send_status = 0;
        }
        catch(IOException e){
            System.out.println("Error in sending data");
        
        }
        
    }
    
    public static void main(String[] args){
    
        try{
           TestClient t = new TestClient();
           t.sendData("send.avi");
        }
        catch(IOException e){
            System.out.println("Error in client stuff");
        }
        }

}
